#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Módulos
import sys, os
import pygame
from pygame.locals import *

# Constantes
WIDTH = 640
HEIGHT = 480
# Clases
# ---------------------------------------------------------------------
class Tux(pygame.sprite.Sprite):

    def __init__(self, uri):
        pygame.sprite.Sprite.__init__(self)
        self.image = load_image(uri, True)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.centery = HEIGHT / 2
        self.speed = [0.5, -0.5]
        
    def update(self, time, object1, object2):

        self.rect.centerx += self.speed[0] * time
        self.rect.centery += self.speed[1] * time

        if self.rect.left <= 0 or self.rect.right >= WIDTH:
            self.speed[0] = -self.speed[0]
            self.rect.centerx += self.speed[0] * time

        if self.rect.top <= 0 or self.rect.bottom >= HEIGHT:
            self.speed[1] = -self.speed[1]
            self.rect.centery += self.speed[1] * time
            
        if pygame.sprite.collide_rect(self, object1):
            self.speed[0] = -self.speed[0]
            self.rect.centerx += self.speed[0] * time
            
        if pygame.sprite.collide_rect(self, object2):
            self.speed[0] = -self.speed[0]
            self.rect.centerx += self.speed[0] * time


class Bar(pygame.sprite.Sprite):

    def __init__(self, x, uri, transparent):

        pygame.sprite.Sprite.__init__(self)
        self.image = load_image(uri, transparent)
        self.rect = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = HEIGHT / 2
        self.speed = 0.5

    def move(self, time, keys):
        if self.rect.top >= 0:
            if keys[K_UP]:
                self.rect.centery -= self.speed * time
        if self.rect.bottom <= HEIGHT:
            if keys[K_DOWN]:
                self.rect.centery += self.speed * time
                
    def ia(self, time, tux):
        if tux.speed[0] >= 0 and tux.rect.centerx >= WIDTH / 2:
            if self.rect.centery < tux.rect.centery:
                self.rect.centery += self.speed * time
            if self.rect.centery > tux.rect.centery:
                    self.rect.centery -= self.speed * time

# ---------------------------------------------------------------------
# Funciones
def load_image(filename, transparent = False):

    try: image = pygame.image.load(filename)
    
    except pygame.error, message:
        raise SystemExit, message
    image = image.convert()

    if transparent:
        color = image.get_at((0, 0))
        image.set_colorkey(color, RLEACCEL)
    return image

def terminate():
    pygame.quit()
    sys.exit()

# ---------------------------------------------------------------------

 

# ---------------------------------------------------------------------

 

def main():
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Pruebas Pygame")
    
    background_image = load_image('images/fondo.jpg')
    
    clock = pygame.time.Clock()

    tux = Tux("images/ball.png")
    bar_player = Bar(15, "images/bar.png", False)
    bar_cpu = Bar(WIDTH - 30, "images/bar.png", False)
    
    display = True

    while display:
        time = clock.tick(60)
        keys = pygame.key.get_pressed()
        
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()            
        if keys[K_p] == False:              
            tux.update(time, bar_player, bar_cpu)
            bar_player.move(time, keys)
            bar_cpu.ia(time, tux)
            screen.blit(background_image, (0, 0))
            screen.blit(tux.image, tux.rect)
            screen.blit(bar_player.image, bar_player.rect)
            screen.blit(bar_cpu.image, bar_cpu.rect)
            pygame.display.flip()
    return 0

 

if __name__ == '__main__':
    pygame.init()
    main()
